const Trade = require('../model/Trade');
const { TradeExpireInterval } = require('../config/TradeBusinessConfig');

/**
 * trade converter between DTO and Model
 */
class TradeConverter {
  /**
   * convert DTO to Model
   * @param {DTO} body
   * @return {Trade} trade
   */
  static createRequestToModel(body) {
    let { price, amount, buy, sell } = body;

    let trade = Trade.build({
      buyOrderId: buy.id,
      sellOrderId: sell.id,
      buyUsername: buy.username,
      sellUsername: sell.username,
      baseCoinType: buy.baseCoinType,
      counterCoinType: buy.counterCoinType,
      price: price,
      sellDepositPrice: sell.depositPrice,
      baseCoinAmount: amount,
      counterCoinAmount: price * amount,
      buyBaseCoinAddress: buy.baseCoinAddress,
      sellBaseCoinAddress: sell.baseCoinAddress,
      buyCounterCoinAddress: buy.counterCoinAddress,
      sellCounterCoinAddress: sell.counterCoinAddress,
      expiredTimestamp:
        Date.now() + TradeExpireInterval[`${buy.baseCoinType}_${buy.counterCoinType}`],
    });

    return trade.get();
  }
}

module.exports = TradeConverter;
